package com.wurao.job;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.wurao.entity.Blog;
import com.wurao.entity.BlogType;
import com.wurao.entity.Blogger;
import com.wurao.entity.Link;
import com.wurao.service.BlogService;
import com.wurao.service.BlogTypeService;
import com.wurao.service.BloggerService;
import com.wurao.service.LinkService;
import com.wurao.util.ResponseUtil;

import net.sf.json.JSONObject;

@Component()
@Lazy(value=false)
public class ZeroDayJob {
	@Autowired
	private ServletContext servletContext;

	@Autowired
	private BloggerService bloggerService;
	
	@Autowired
	private LinkService linkService;
	
	@Autowired
	private BlogTypeService blogTypeService;
	
	@Autowired
	private BlogService blogService;
	
	@Scheduled(cron = "0 30 0 * * ?")//每天凌晨0点30分
    public void test() throws Exception {
        System.out.println("Test is working......");
        Blogger blogger=bloggerService.find(1); // 获取博主信息
		blogger.setPassword(null);
		servletContext.setAttribute("blogger", blogger);
		
		List<Link> linkList=linkService.listLink(null); // 查询所有的友情链接信息
		servletContext.setAttribute("linkList", linkList);
		
		List<BlogType> blogTypeCountList=blogTypeService.countList(); // 查询博客类别以及博客的数量
		servletContext.setAttribute("blogTypeCountList", blogTypeCountList);
		
		List<Blog> blogCountList=blogService.countList(); // 根据日期分组查询博客
		servletContext.setAttribute("blogCountList", blogCountList);
		
		List<Blog> blogTopCountList=blogService.topCountList();//查询热门文章
		servletContext.setAttribute("blogTopCountList",blogTopCountList);
		
		System.out.println("over....");
		
		SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间
		Date date;
		Date date2;
		try {
			date = sDateFormat.parse("2016-05-30 23:59:59");
			date2 = new Date();//现在的时间
			long diff = date2.getTime() - date.getTime();//这样得到的差值是微秒级别  
		    long days = diff / (1000 * 60 * 60 * 24);    
		    long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);  
		    long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);  
//		    System.out.println(	days+"天"+hours+"小时"+minutes+"分");  
//		    System.out.println(days/365+"年"+days%365/30+"月");
		    servletContext.setAttribute("days",days+"天"+hours+"小时"+minutes+"分");
		    servletContext.setAttribute("years",days/365+"年"+(days%365/30+1)+"月");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    //@Scheduled(cron = "*/5 * * * * ?")//每隔5秒执行一次
    //@Scheduled(cron = "0 0 1 * * ?")//每天凌晨1点整
    //@Scheduled(cron = "0 30 0 * * ?")//每天凌晨0点30分
    //@Scheduled(cron = "0 */60 * * * ?")//1小时处理一次
}
